<template>
	<view>
		<view class='UCenter-bg'>
			<!-- <image src='/images/logo.png' class='png' mode='widthFix'></image> -->

		</view>
		<view class='padding-xl'>
			<view class='text-xl'>请点击下方按钮允许微信授权，进入出入登记系统
			</view>
			<button class='cu-btn  bg-green shadow lg block' open-type="getUserInfo" @getuserinfo="onGetUserInfo">微信登录授权</button>
			<!-- <button class='cu-btn  bg-gray  shadow lg block'  >取消微信登录授权</button> -->
		</view>
	</view>
</template>

<script>
	import uniSegmentedControl from '@/components/uni-segmented-control/uni-segmented-control.vue';
	import {
		post,
		get,
		showModal
	} from '../../utils/index.js';
	import {
		Base64
	} from 'js-base64';
	var that;
	export default {
		data() {
			return {
				authData: {}
			}
		},
		onLoad() {

		},
		onShow: function() {
			wx.checkSession({ //检测当前用户的session_key是否过期
				success: function() { //session_key 未过期，并且在本生命周期一直有效
					//console.log("授权未过期")
					uni.reLaunch({
						url: '../index/index'
					});
					return;
				}
			})
		},
		methods: {
			onGetUserInfo(data) {
				wx.showLoading({
					title: "登录中"
				});
				that = this;
				uni.login({
					//获取code
					success: (res) => {
						let code = res.code;
						uni.getUserInfo({
							success: (uinfo) => {
								uni.setStorageSync('avatarUrl',uinfo.userInfo.avatarUrl)
								//请求授权
								post(that.weixin_app + '/wxapp/auth', {
									encryptedData: uinfo.encryptedData,
									iv: uinfo.iv,
									code: code,
									spread: 0
								}).then(info => {
									
									uni.setStorageSync('pid', info.data.token);
									uni.setStorageSync('token', info.data.token);
									uni.setStorageSync('uid', info.data.uid);
									if(uni.getStorageSync("toadd") === 1){
										uni.removeStorageSync('toadd');
										uni.reLaunch({
											url: '../add/add?id='+uni.getStorageSync("shequ_id")+'&shequ_name= '+uni.getStorageSync("shequ_name")+'&type='+uni.getStorageSync("shequ_type")
										});
										
									}else{
										uni.setStorageSync('shequ_id', 1);
										uni.setStorageSync('shequ_name', '军转办小区125号');
										uni.reLaunch({
											url: '../index/index'
										});
										return false;
									}
									
								})
							}
						})
					}
				})

			}
		}
	}
</script>

<style>
	.UCenter-bg {
		background-image: url(https://image.weilanwl.com/color2.0/index.jpg);
		background-size: cover;
		height: 700rpx;
		display: flex;
		justify-content: center;
		overflow: hidden;
		position: relative;
		flex-direction: column;
		align-items: center;
		color: #fff;
		font-weight: 300;
		text-shadow: 0 0 3px rgba(0, 0, 0, 0.3);
	}

	.UCenter-bg::after {
		content: "";
		position: absolute;
		width: 100vw;
		height: 50vw;
		background-color: #f1f1f1;
		transform: rotate(-10deg) scale(2, 2);
		bottom: -60vw;
		left: 0;
		right: 0;
		margin: auto;

	}

	.UCenter-bg text {
		opacity: 0.8;
	}

	.UCenter-bg image {
		width: 250rpx;
		height: 250rpx;
	}
</style>
